//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.jeewx.api.core.handler.impl;

import com.google.gson.Gson;
import org.jeewx.api.core.annotation.ReqType;
import org.jeewx.api.core.exception.WexinReqException;
import org.jeewx.api.core.handler.WeiXinReqHandler;
import org.jeewx.api.core.req.model.WeixinReqConfig;
import org.jeewx.api.core.req.model.WeixinReqParam;
import org.jeewx.api.core.req.model.message.IndustryTemplateMessageSend;
import org.jeewx.api.core.req.model.message.TemplateMessage;
import org.jeewx.api.core.util.HttpRequestProxy;
import org.jeewx.api.core.util.WeiXinReqUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class WeixinReqTemplateMessageHandler implements WeiXinReqHandler {
    private static Logger logger = LoggerFactory.getLogger(WeixinReqTemplateMessageHandler.class);

    public WeixinReqTemplateMessageHandler() {
    }

    public String doRequest(WeixinReqParam weixinReqParam) throws WexinReqException {
        String strReturnInfo = "";
        if (weixinReqParam.getClass().isAnnotationPresent(ReqType.class)) {
            ReqType reqType = (ReqType)weixinReqParam.getClass().getAnnotation(ReqType.class);
            WeixinReqConfig objConfig = WeiXinReqUtil.getWeixinReqConfig(reqType.value());
            if (objConfig != null) {
                String reqUrl = objConfig.getUrl();
                IndustryTemplateMessageSend mc = (IndustryTemplateMessageSend)weixinReqParam;
                Map<String, String> parameters = new HashMap();
                parameters.put("access_token", mc.getAccess_token());
                String jsonData = this.getMsgJson(mc);
                logger.info("处理模板消息" + jsonData);
                strReturnInfo = HttpRequestProxy.doJsonPost(reqUrl, parameters, jsonData);
            }
        } else {
            logger.info("没有找到对应的配置信息");
        }

        return strReturnInfo;
    }

    private String getMsgJson(IndustryTemplateMessageSend mc) {
        StringBuffer json = new StringBuffer();
        Gson gson = new Gson();
        TemplateMessage tm = mc.getData();
        mc.setData((TemplateMessage)null);
        String objJson = gson.toJson(mc);
        mc.setData(tm);
        json.append(objJson);
        json.setLength(json.length() - 1);
        json.append(",");
        json.append("\"data\":{");
        objJson = gson.toJson(tm.getFirst());
        json.append(" \"first\":");
        json.append(objJson);
        json.append(",");
        objJson = gson.toJson(tm.getKeynote1());
        json.append(" \"keyword1\":");
        json.append(objJson);
        json.append(",");
        objJson = gson.toJson(tm.getKeynote2());
        json.append(" \"keyword2\":");
        json.append(objJson);
        json.append(",");
        objJson = gson.toJson(tm.getKeynote3());
        if (null != objJson) {
            json.append(" \"keyword3\":");
            json.append(objJson);
            json.append(",");
        }
        objJson = gson.toJson(tm.getKeynote4());
        if (null != objJson) {
            json.append(" \"keyword4\":");
            json.append(objJson);
            json.append(",");
        }

        objJson = gson.toJson(tm.getKeynote5());
        if (null != objJson) {
            json.append(" \"keyword5\":");
            json.append(objJson);
            json.append(",");
        }

        objJson = gson.toJson(tm.getRemark());
        json.append(" \"remark\":");
        json.append(objJson);
        json.append("}}");
        return json.toString();
    }
}
